package com.kmair.caiwu.financialexpress.service.evaluator;

import com.kmair.caiwu.financialexpress.pojo.ExpreseData;
import com.kmair.caiwu.financialexpress.pojo.FinancialExpressFormation;
import com.kmair.caiwu.financialexpress.service.DataEvalServic;
import com.kmair.caiwu.financialexpress.service.impl.ExpreseDataService;
import com.kmair.caiwu.financialexpress.util.StringEval;
import com.kmair.caiwu.financialexpress.util.impl.ExpressDataChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExpreseDataEval implements DataEvalServic {
    private final ExpreseDataService eds;
    private final ExpressDataChange edc;

    @Autowired
    public ExpreseDataEval(ExpreseDataService eds, ExpressDataChange edc) {
        this.eds = eds;
        this.edc = edc;
    }

    @Override
    public Double evalSummary(String subjectId, String qureyDate) {
        return eds.sumActualPrice(subjectId, qureyDate);
    }

    @Override
    public Double evalString(Object entity) {
        StringEval se = new StringEval(edc);
        if (((ExpreseData) entity).getFinancialExpressFormation().getCalculationFormula() == null) {
            entity = eds.selectSelective(entity).get(0);
        }
        String calculationFormula = ((ExpreseData) entity).getFinancialExpressFormation().getCalculationFormula();//获取科目的计算公式
        String lien = se.stringToEvalString(calculationFormula, entity);//将科目中储存的名称计算公式转为对应值的计算
        return Double.valueOf(se.evalData(lien));
    }

    @Override
    public void valueCalculate(String queryDate) {
        List<List<ExpreseData>> edList = new ArrayList();
        //获取各级目录
        for (int i = 7; i >= 1; i -= 2) {
            edList.add(eds.selectBySubjectIdLength(i, queryDate));
        }
        for (List<ExpreseData> edl : edList
                ) {
            for (ExpreseData edf : edl) {
                edf.setActualData(selectCalculate(edf));
                eds.updateByPrimaryKeySelective(edf);
            }
        }
    }

    public Double selectCalculate(ExpreseData ed) {
        switch (ed.getFinancialExpressFormation().getParamProperty()) {
            case "1":
                return evalString(ed);
            case "3":
                return evalSummary(ed.getSubjectId(), ed.getActualDate());
        }
        return ed.getPlanData();
    }

    /**
     * 数据初始化工作，先生成当月空数据，再要根据计算公式（标识为2）计算出初始值。
     *
     * @param queryDate
     */
    public void expreseDataInit(String queryDate) {
        ExpreseData ed = new ExpreseData();
        FinancialExpressFormation fef = new FinancialExpressFormation();
        ed.setActualDate(queryDate);
        fef.setParamProperty("2");
        ed.setFinancialExpressFormation(fef);
        List<ExpreseData> edlist = eds.selectSelective(ed);
        if (edlist.size() == 0) {
            eds.expreseDataInit(queryDate);//数据初始化
            edlist = eds.selectSelective(ed);
        }
        for (ExpreseData ed1 : edlist
                ) {
            ed1.setActualData(evalString(ed1));
            eds.updateByPrimaryKeySelective(ed1);
        }
    }
}
